package fair.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import fair.dao.PermissionDao;
import fair.dao.RoleDao;
import fair.dao.UserDao;
import fair.pojo.Permission;
import fair.pojo.Role;
import fair.pojo.User;
import fair.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author fair_wu
 * @version v1.0
 * @package fair.service.impl
 * @date 2021-02-08 下午 17:02
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public User findByUsername(String username) {
        //查询用户基本信息
        User user = userDao.findByUsername(username);
        if (user == null)
            return null;
        //根据用户id查询用户角色 set集合不允许重复
        Integer id = user.getId();
        Set<Role> roles = roleDao.findByUserId(id);
        if (roles != null) {
            for (Role role : roles) {
                //根据用户角色id查询用户权限 set集合不允许重复
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if (permissions != null) {
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;
    }

}
